package dymnd.ui;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseEvent;

import javax.swing.JViewport;

public class DragViewport extends JViewport{

    
    int lastX = 0;
    int lastY = 0;
    boolean dragStart = true;
    
    
    
    
    public DragViewport()    {
        super();
    }
    
    public void mouseDragged(MouseEvent e)  {
        //System.out.println("Viewport dragging");
        if(dragStart)   {
            lastX = e.getX();
            lastY = e.getY();
        }
        
        Component view = getView();
        Point p = getViewPosition();
        int x  = p.x - (e.getX() - lastX);
        int y = p.y - (e.getY() - lastY);
        
        
        if(view.getWidth() >= getWidth())
	        if(x + getExtentSize().getWidth() > view.getWidth())	{
	        	x = (int) (view.getWidth() - getExtentSize().getWidth());
	        }
        
        if(y + getExtentSize().getHeight() > view.getHeight())	{
        	y = (int) (view.getHeight() - getExtentSize().getHeight());
        }
        
        if(x < 0)
        	x = 0;
        
        if(y < 0)
        	y = 0;
        
        setViewPosition(new Point(x, y));
        
        
        lastX = e.getX();
        lastY = e.getY();
        dragStart = false;
        repaint();
    }
    
    
    
    //Dimension extent;
   /* public Dimension getExtentSize()	{
    	extent = new Dimension((int)(getView().getWidth() - getViewPosition().getX()), (int)(getView().getHeight() - getViewPosition().getY()));
    	return extent;
    }*/
    
    public void mouseReleased(MouseEvent e) {
         dragStart = true;
    }
    
}